<?
require_once(dirname(__FILE__).'/../models/Users.php');
require_once(dirname(__FILE__).'/../models/Statuses.php');

class UsersController extends Main_Controller_Action {
	public function preDispatch()
    {
        if (!$this->loggedUser->getIsAdministrator() && !$this->loggedUser->getIsOperator()) {        
                $this->_helper->redirector('', '');
        } else {
           /**
			 * Translation - Loading dictionaries common and backoffice_common
			 */
			$commonLocale = Translator::loadTranslation($this->currentLang, 'common');
			$this->view->commonLocale = $commonLocale;
        }
    }
    
	function indexAction(){
		
		
		try{
			$users = ModelMapper::instance('Users')->findFiltered('','ORDER BY last_name ASC');	
		} catch (DatabaseException $e){
			$e->getMessage();
		}

		$this->view->users = $users;
	}
	
	function addAction(){
		$filter = "WHERE sc_catalogue = 'users'";
		$orderBy = "ORDER BY status ASC";
		$statuses = ModelMapper::instance('Statuses')->findFiltered($filter, $orderBy);
		
		$smarty = $this->view->getEngine();
		$this->view->statuses = $statuses;
		$this->view->apps = false;
		
		$tpl = $smarty->fetch('users/add.tpl');
		
		$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		$this->getResponse()->sendResponse();
		exit();

	}
	
	function editAction(){
		$user = ModelMapper::instance('Users')->find(intval($this->getRequest()->getParam('usersId')));
		
		$filter = "WHERE sc_catalogue = 'users'";
		$orderBy = "ORDER BY status ASC";
		$statuses = ModelMapper::instance('Statuses')->findFiltered($filter, $orderBy);
		
		$smarty = $this->view->getEngine();
		
		$this->view->statuses = $statuses;
		$this->view->user = $user;
		
		$tpl = $smarty->fetch('users/edit.tpl');
		
		$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		$this->getResponse()->sendResponse();
		exit();
		
	}
	
	function deleteAction(){
		$user = ModelMapper::instance('Users')->find(intval($this->getRequest()->getParam('usersId')));
		$user->delete();
		exit();
	}
	
	function saveAction(){
		//Database::instance()->getConn()->debug=1;
		
		$formData = $this->_request->getParam('addForm');
		
		if (intval($formData['usersId']) >0)
			$obj = ModelMapper::instance('Users')->find(intval($formData['usersId']));
		else
			$obj = new Users();
		$obj->setAppsId($this->view->appsId);
		$obj->setUsername(trim(strip_tags(stripcslashes($formData['username']))));
		$obj->setFirstName(trim(strip_tags(stripcslashes($formData['firstName']))));
		$obj->setLastName(trim(strip_tags(stripcslashes($formData['lastName']))));
		
		if (!intval($formData['usersId']) >0){
			$obj->setPassword(md5(trim($formData['password'])));
			$obj->setRegistrationDate(date('Y-m-d H:i:s'));
			$obj->setIsAdministrator(0);
		}
		if (!empty($formData['isOperator']))
			$obj->setIsOperator(intval($formData['isOperator']));
		else 
			$obj->setIsOperator(0);
		
		$obj->setEmail(trim(strip_tags(stripcslashes($formData['email']))));
		$obj->setStatusesId(intval($formData['statusesId']));
		$obj->setSystemRolesId(0);
		$obj->setSaddrFirstName($formData['sAddr']['firstName']);
		$obj->setSaddrLastName($formData['sAddr']['lastName']);
		$obj->setSaddrEmail($formData['sAddr']['email']);
		$obj->setSaddrStreet($formData['sAddr']['street']);
		$obj->setSaddrBuildingNo($formData['sAddr']['buildingNo']);
		$obj->setSaddrApartmentNo($formData['sAddr']['apartmentNo']);
		$obj->setSaddrPostcode($formData['sAddr']['postcode']);
		$obj->setSaddrCity($formData['sAddr']['city']);
		$obj->setSaddrCountry($formData['sAddr']['country']);
		$obj->setBaddrCompanyName($formData['bAddr']['companyName']);
		$obj->setBaddrFirstName($formData['bAddr']['firstName']);
		$obj->setBaddrLastName($formData['bAddr']['lastName']);
		$obj->setBaddrEmail($formData['bAddr']['email']);
		$obj->setBaddrStreet($formData['bAddr']['street']);
		$obj->setBaddrBuildingNo($formData['bAddr']['buildingNo']);
		$obj->setBaddrApartmentNo($formData['bAddr']['apartmentNo']);
		$obj->setBaddrPostcode($formData['bAddr']['postcode']);
		$obj->setBaddrCity($formData['bAddr']['city']);
		$obj->setBaddrCountry($formData['bAddr']['country']);
		$obj->setBaddrVatId('');
		$obj->setRemarks('');
	
		exit();
	}
}
?>